package main

import (
	"net"
	"fmt"
	"bufio"
	"os"
	"strings"
)

func main() {
	// make address
	tcpAddr, err := net.ResolveTCPAddr("tcp", "localhost:2999")
	if err!=nil {
		fmt.Println(err.Error())
	}

	// connect
	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	if err != nil {
		fmt.Println(err.Error())
	}

	reader := bufio.NewReader(os.Stdin)
	for {
		// read from stdin
		fmt.Print("Enter text: ")
		text, _ := reader.ReadString('\n')
		// request - send something
		_, err = conn.Write([]byte(strings.Trim(text, "\n")))
		if err != nil {
			fmt.Println(err.Error())
		}

		// response - receive the same content
		response := make([]byte, 100)
		n, err := conn.Read(response)
		if err != nil {
			fmt.Println(err.Error())
		}
		fmt.Println(string(response[:n]))
	}



}
